<?php
include("check.php");
include("check2.php");
include_once("../function.php");
include_once("../class/system_class.php");
include_once("../class/ulevel_class.php");
include_once ("../bonus.php");

header("Content-Type: text/html;charset=utf-8");
session_start();

$system=new system_class();
$ulevel=new ulevel_class();
$_sys=$system->system_information(1);

$us=que_select_cl('member',$_SESSION['ID']);
$_iul=$ulevel->getulevelbyulevel($us['ulevel']);
if ($_POST['submit']){
    /*    if($us['isbd']==2){*/
    $uid=$_SESSION['ID'];
    $jine=$_POST['jine'];
    if ($jine>0){
        $ze=0;
        if ($_POST['lx']==0 ){
            $ze=$us['mey'];
        }elseif ($_POST['lx']==1){
            $ze=$us['zsq'];
        }elseif ($_POST['lx']==3){
            $ze=$us['gwb'];
        }elseif($_POST['lx']==4){
            $ze=$us['cfxf'];
        }
        if ($ze>=$jine){
            if ($_POST['lx']==0){
                $wlf = $_sys['wlf']/100;//实体基金
                $ls = $_sys['sl']/100;//公益基金
                $jmf1 = $_sys['jmf1']/100;//消费积分
                $chongfuxiaofei = $_sys['chongfuxiaofei']/100;//服务费

                $csjjmey=$jine*$jmf1;//消费积分
                $st=$jine*$wlf; //实体基金
                $zhglf=$jine*$ls; //公益基金
                $fuwufei=$jine*$chongfuxiaofei;//服务费
                $sjmey=$jine*(1-$wlf-$ls-$jmf1-$chongfuxiaofei);
                $us_update['mey'] = $us['mey'] - $jine;
                $us_update['pv'] = $us['pv'] + $st;
                $us_update['zsq'] = $us['zsq'] + $sjmey;
                $us_update['chr2'] = $us['chr2'] + $csjjmey;

            }elseif($_POST['lx']==1){
                $us_update['zsq']=$us['zsq']-$jine;
                $us_update['mey']=$us['mey']+$jine;
            }elseif($_POST['lx']==2){
                $us_update['zsq']=$us['zsq']-$jine;
                $us_update['gwb']=$us['gwb']+$jine;
            }elseif($_POST['lx']==3){
                $us_update['gwb']=$us['gwb']-$jine;
                $us_update['zsq']=$us['zsq']+$jine;
            }elseif($_POST['lx']==4){
                if($_iul['yl10']<$us['pv']+$jine/2){
                    $cha=$_iul['yl10']-$us['pv'];
                    if($cha<$jine/2){
                        $us_update['cfxf']=$us['cfxf']-$jine;
                        $us_update['pv']=$us['pv']+$cha;
                    }
                }else{
                    $us_update['cfxf']=$us['cfxf']-$jine;
                    $us_update['pv']=$us['pv']+$jine/2;
                }
            }
            edit_update_cl('member',$us_update,$us['id']);


            $zhuanhuan['uid']=$uid;
            $zhuanhuan['nickname']=$_SESSION['nickname'];
            $zhuanhuan['username']=$us['username'];
            $zhuanhuan['jine']=$jine;
        //    $zhuanhuan['jinzhongzi']=$fuwufei;//服务费
        //    $zhuanhuan['zonghe'] = $zhglf;//公益基金
       ////     $zhuanhuan['st'] = $st;//实体基金
       //     $zhuanhuan['csjjmey'] = $csjjmey;//消费积分
            $zhuanhuan['zdate']=now();
            $zhuanhuan['lx']=$_POST['lx'];
            //       $zhuanhuan['sxf'] = $sxfmey;//手续费
            //        $zhuanhuan['szzcmey'] = $szzcmey;//数字资产
            //          $zhuanhuan['xfjfmey'] = $xfjfmey;//消费积分
            add_insert_cl('zhuanhuan',$zhuanhuan);
            echo "<script language=javascript>alert('货币转换成功.\\n本次转换金额:".$jine."');window.location.href='?'</script>";
        }else{
            echo "<script language=javascript>alert('您的余额不足,无法转换.');window.location.href='?'</script>";
        }
    }else{
        echo "<script language=javascript>alert('转换金额不正确,请确认后重新转换');window.location.href='?'</script>";
    }
    /*  }else{
          echo "<script language=javascript>alert('你不是服务中心,不能转换');window.location.href='?'</script>";
      }*/

}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/table.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <title>货币转换</title>
    <script language="javascript">
        <!--
        function select() {
            lx=document.form1.lx.value;
            if(lx=="0"){
                document.getElementById('a1').style.display="";
                document.getElementById('a2').style.display="";
                document.getElementById('z1').style.display="none";
                document.getElementById('z2').style.display="none";
            }else if(lx=="4"){
                document.getElementById('a1').style.display="none";
                document.getElementById('a2').style.display="none";
                document.getElementById('z1').style.display="";
                document.getElementById('z2').style.display="";
            }
        }
        function CheckForm(){
            jine=document.form1.jine.value;
            if(jine.length == 0){
                alert("温馨提示:\n请输入转换金额.");
                document.form1.jine.focus();
                return false;
            }
            if(jine <= 0){
                alert("温馨提示:\n转换金额必须大于0.");
                document.form1.jine.focus();
                return false;
            }

            if(jine % <?=$_sys['fanli']?> != 0){
                alert("温馨提示:\n转换金额必须为<?=$_sys['fanli']?>的倍数.");
                document.form1.jine.focus();
                return false;
            }
            return true;
        }
        -->
    </script>
</head>
<body>
<form name="form1" method="post" action="?" onSubmit="return CheckForm();">
    <table  width="100%" cellpadding="3" cellspacing="1" border="0" align="center" class="table1">
        <tr id="a1">
            <td width="46%" height="22" align="right">奖金积分余额：</td>
            <td width="54%" align="left"><?=$us['mey']?></td>
        </tr>
        <!--  <tr>
    <td width="46%" height="22" align="right">电子币余额：</td>
    <td width="54%" align="left"><?/*=$us['gwb']*/?></td>
  </tr>-->
        <tr id="a2" >
            <td width="46%" height="22" align="right">报单积分余额：</td>
            <td width="54%" align="left"><?=$us['zsq']?></td>
        </tr>
        <tr id="z1" style="display: none">
            <td width="46%" height="22" align="right">专用币余额：</td>
            <td width="54%" align="left"><?=$us['cfxf']?></td>
        </tr>
        <tr id="z2" style="display: none">
            <td width="46%" height="22" align="right">积分余额：</td>
            <td width="54%" align="left"><?=$us['pv']?></td>
        </tr>
        <tr>
            <td width="46%" height="22" align="right">转换类型：</td>
            <td width="54%" align="left">
                <select name="lx" id="lx" onchange="select()" >
                    <?php if($zhison1==1){?>
                        <option value="0"><?=$zhuanghuanlx1?></option>
                    <?php }?>
                    <?php if($zhison2==1){?>
                        <option value="1"><?=$zhuanghuanlx2?></option>
                    <?php }?>
                    <!--        <option value="1">充值积分转奖金币</option>-->
                    <!-- <option value="1">奖金转购物币</option>-->
                    <!--     <option value="2">充值积分转电子币</option>-->


                </select></td>
        </tr>
        <tr>
            <td height="22" align="right">转换金额：</td>
            <td align="left"><input name="jine" type="text" id="jine" size="10" maxlength="10"></td>
        </tr>
    </table>
    <table align="center">
        <tr>
            <td><input name="submit" id="submit" type="submit" class="button_green" value="转  换"></td>
        </tr>
    </table>
    <br>
    <table width="100%" cellpadding="3" cellspacing="1" border="0" align="center" class="table1">
        <tr>
            <td height="20" colspan="16" align="center"> 转 换 记 录</td>
        </tr>
        <tr>
            <td align="center">会员编号</td>
            <td align="center">会员姓名</td>
            <td align="center">转换金额</td>
            <td align="center">服务费</td>
            <td align="center">公益基金</td>
            <td align="center">消费积分</td>
            <td align="center">实体基金</td>
            <td align="center">转换类型</td>
            <td align="center">转换时间</td>

        </tr>
        <?php
        $pagesize = 10; //设置每页记录数
        $sql = "SELECT * FROM `zhuanhuan` WHERE uid=".$_SESSION['ID']."";
        if($query = mysql_query($sql)){
            $sum = mysql_num_rows($query); //计算总记录数
        }else{
            $sum=0;
        }
        if($sum % $pagesize == 0) //计算总页数
            $total = (int)($sum/$pagesize);
        else
            $total = (int)($sum/$pagesize) + 1;
        if (isset($_GET['page'])) //获得页码
        {
            $p = (int)$_GET['page'];
        }
        else
        {
            $p = 1;
        }
        if ($p>$total){
            $p=$total;
        }
        $start = $pagesize * ($p - 1); //计算起始记录
        $sql = "SELECT * FROM `zhuanhuan` WHERE uid=".$_SESSION['ID']." order by id desc limit ".$start.",".$pagesize;
        if($query = mysql_query($sql)){
            while ($row=mysql_fetch_array($query)){
                ?>
                <tr>
                    <td align="center"><?=$row['nickname']?></td>
                    <td align="center"><?=$row['username']?></td>
                    <td align="center"><?=$row['jine']?></td>
                    <td align="center"><?=$row['jinzhongzi']?></td>
                    <td align="center"><?=$row['zonghe']?></td>
                    <td align="center"><?=$row['csjjmey']?></td>
                    <td align="center"><?=$row['st']?></td>
                    <td align="center"><?php if ($row['lx']==0){ echo $zhuanghuanlx1; }elseif($row['lx']==1){ echo $zhuanghuanlx2; }elseif($row['lx']==2){ echo $zhuanghuanlx3; }elseif($row['lx']==3){?>电子币转充值积分<?php }elseif($row['lx']==4){?>专用币转积分<?php }?></td>
                    <td align="center"><?=$row['zdate']?></td>

                </tr>
                <?php
            }
        }
        ?>
    </table>
    <table width="100%" border="0" class="ziti">
        <tr>
            <td align="left">&nbsp;</td>
            <td align="right"><?php echo fenye($p,$pagesize,$sum,$total,$cx)?></td>
        </tr>
    </table>
</form>
</body>
</html>